/**
 * @class WebOs.application.DefaultApplication
 * @markdown 默认应用，描述了一个应用启动的一般流程，其他运用应继承该类并覆盖其中的showLoginFrame和destroyLoginFrame函数
 */
Ext.define('WebOs.application.DefaultApplication', {
			version : '1.0',
			name : '小区管理系统',

			mixins : {
				observable : 'Ext.util.Observable'
			},

			constructor : function(config) {
				var me = this;
				document.title = me.name;
				me.mixins.observable.constructor.call(this, config);
				me.addEvents('started');
				core.addListener('userlogin', me.onUserLogin);
				core.addListener('servicemounted',me.onServiceMounted);
			},
			
			run : function(config) {
				var me = this;
				Ext.applyIf(me, config);
				core.checkLogin(me.isLogined,me.noLogin);
			},
			
			isLogined : function(){
				core.initUserService();
			},
			
			noLogin : function(){
				core.application.showLoginFrame();
			},

			onUserLogin : function(user) {
				var me = this.application;
				me.destroyLoginFrame(); 
				core.initUserService();
			},
			
			onServiceMounted : function(){
				var me = this.application;
				core.initView();
				SessionPermissionPlugin = Ext.create('WebOs.ex.PermissionPlugin');
				me.fireEvent('started', me);
			},

			/**
			 * 展示登陆界面
			 */
			showLoginFrame : function() {
				var  height = document.documentElement.clientHeight;
				document.body.onresize = function(){
					var newHeight = document.documentElement.clientHeight;
					document.getElementById('login_frame').height = newHeight;
				}; 
				Ext.fly(document.body).insertHtml('afterBegin','<iframe style="border:none" width="100%" height="'+height+'" id="login_frame" src="login.jsp"></iframe>');
				var iframe = document.getElementById('login_frame');
				if(iframe.attachEvent){
					iframe.attachEvent('onload',this.bindLogin);
				}else{
					iframe.onload = this.bindLogin;
				}
			},
			
			bindLogin : function(){
				var iframeDoc = document.getElementById('login_frame').contentDocument;
				var subBtn = iframeDoc.getElementById('sub_btn');
				subBtn.onclick = function(){
					var uname = iframeDoc.getElementById('userName').value;
					var upasswd = iframeDoc.getElementById('password').value;
					var verifyCode = iframeDoc.getElementById('verifyCode').value;
					core.login(uname,upasswd,verifyCode,null,function(msg){
						alert(msg);
					});
				};
			},
			
			/**
			 * 销毁登陆界面
			 */
			destroyLoginFrame : function(){
				document.body.removeChild(document.getElementById('login_frame'));
				document.body.onresize = null;
			}

		});